package com.shop2cn.iapi.coupon.testcase.old;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.coupon.parameter.CouponInfo;
import com.shop2cn.iapi.coupon.parameter.CouponInfos;
import com.shop2cn.iapi.coupon.parameter.GetCouponListByCodeBean;
import com.shop2cn.iapi.coupon.parameter.UserReceiveCouponBean;
import com.shop2cn.iapi.coupon.service.GetCouponListByCodeCall;
import com.shop2cn.iapi.coupon.service.UserReceiveCouponCall;
import com.shop2cn.iapi.couponmanage.parameter.CreateCouponBean;
import com.shop2cn.iapi.couponmanage.parameter.SellerCreateBatchBean;
import com.shop2cn.iapi.couponmanage.service.CreateCouponCall;
import com.shop2cn.iapi.couponmanage.service.SellerCreateBatchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.CouponShop2cnMCallService;
import com.ymttest.database.model.couponusecondition;
import com.ymttest.database.sqlwapper.couponWapper;
import com.ymttest.database.sqlwapper.couponbatchWapper;
import com.ymttest.database.sqlwapper.couponuseconditionWapper;
import com.ymttest.utils.TestDataManager;
//import com.ymttest.database.sqlwapper.couponbatchWapper;
//import com.ymttest.database.sqlwapper.YmtCouponWapper;
import com.ymttest.utils.YMTDateUtil;

@TestSuite
public class Ts_GetCouponListByCode {
	private static GetCouponListByCodeBean getcouponlistbycodeBean;
    private static GetCouponListByCodeCall getcouponlistbycodeCall;

    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("根据优惠券编号获取优惠券信息用例");
    }

    @Before
    public void caseUp() {
        getcouponlistbycodeBean = new GetCouponListByCodeBean();
        getcouponlistbycodeCall = new GetCouponListByCodeCall();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(getcouponlistbycodeCall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetCouponListByCode_001() {
        Logger.start(true, "根据优惠券编号获取优惠券信息-平台短码优惠券");
        try {
            HashMap<String, Map> couponInfoMap = new HashMap<String, Map>();
            List<String> couponCodesList = new ArrayList<String>();
            //AcquireMethod 1:直接领取(默认)  2:兑换 3:店铺分享券
            List<Map> couponInfo2=new couponWapper().selectCouponInfoByAcquireMethod(2);
 
            if(couponInfo2.size()>0)
            {
            	for(int i=0;i<couponInfo2.size();i++)
            	{
                    String couponCode2 = couponInfo2.get(i).get("CouponCode").toString();
            		 couponInfoMap.put(couponCode2, couponInfo2.get(i));
            		couponCodesList.add(couponCode2);
            	}
                getcouponlistbycodeBean.setCouponCodes(couponCodesList);
                getcouponlistbycodeCall.setData(getcouponlistbycodeBean);
                getcouponlistbycodeCall.callService();

                verifyReturn(couponInfoMap);
            }
            else
            {
            	Logger.verifyEquals(false, true, "没有找到对应的兑换码优惠券编码");
            }
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetCouponListByCode_002() {
        Logger.start(true, "根据优惠券编号获取优惠券信息-平台链接优惠券(若productLimitType=2，指定商品，则商品id(specificProducts)不返回)");
        try {
            HashMap<String, Map> couponInfoMap = new HashMap<String, Map>();
            List<String> couponCodesList = new ArrayList<String>();
            couponbatchWapper couponbatchWapper = new couponbatchWapper();

            // 平台链接优惠券
            List<Map> couponInfo2 = couponbatchWapper.getTop1PlatformLinkCouponInfo();
            String couponCode2 = couponInfo2.get(0).get("CouponCode").toString();
            couponCodesList.add(couponCode2);
            couponInfoMap.put(couponCode2, couponInfo2.get(0));

            getcouponlistbycodeBean.setCouponCodes(couponCodesList);
            getcouponlistbycodeCall.setData(getcouponlistbycodeBean);
            getcouponlistbycodeCall.callService();

            verifyReturn(couponInfoMap);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetCouponListByCode_003() {
        Logger.start(true, "根据优惠券编号获取优惠券信息-商家全场优惠券");
        try {
            HashMap<String, Map> couponInfoMap = new HashMap<String, Map>();
            List<String> couponCodesList = new ArrayList<String>();
            couponbatchWapper couponbatchWapper = new couponbatchWapper();

            // 商家全场优惠券
            List<Map> couponInfo3 = couponbatchWapper.getTop1SellerCouponInfo();
            couponCodesList.add(couponInfo3.get(0).get("CouponCode").toString());
            String couponCode3 = couponInfo3.get(0).get("CouponCode").toString();
            couponCodesList.add(couponCode3);
            couponInfoMap.put(couponCode3, couponInfo3.get(0));

            getcouponlistbycodeBean.setCouponCodes(couponCodesList);
            getcouponlistbycodeCall.setData(getcouponlistbycodeBean);
            getcouponlistbycodeCall.callService();

            verifyReturn(couponInfoMap);
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    @Ignore
    // 商家券不能指定商品
    public void Tc_GetCouponListByCode_004() {
        Logger.start(true, "根据优惠券编号获取优惠券信息-商家商品优惠券");
        try {
            HashMap<String, Map> couponInfoMap = new HashMap<String, Map>();
            List<String> couponCodesList = new ArrayList<String>();
            couponbatchWapper couponbatchWapper = new couponbatchWapper();

            // 商家商品优惠券
            List<Map> couponInfo4 = couponbatchWapper.getTop1SellerProductCouponInfo();
            couponCodesList.add(couponInfo4.get(0).get("sCouponCode").toString());
            String couponCode4 = couponInfo4.get(0).get("sCouponCode").toString();
            couponCodesList.add(couponCode4);
            couponInfoMap.put(couponCode4, couponInfo4.get(0));

            getcouponlistbycodeBean.setCouponCodes(couponCodesList);
            getcouponlistbycodeCall.setData(getcouponlistbycodeBean);
            getcouponlistbycodeCall.callService();

            verifyReturn(couponInfoMap);
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    @Ignore
    public void Tc_GetCouponListByCode_005() {
        Logger.start(true, "根据优惠券编号获取优惠券信息-混合优惠券");
        try {
            HashMap<String, Map> couponInfoMap = new HashMap<String, Map>();
            List<String> couponCodesList = new ArrayList<String>();
            couponbatchWapper couponbatchWapper = new couponbatchWapper();

            // 平台短码优惠券
            List<Map> couponInfo1 = couponbatchWapper.getTop1PlatformShortCodeCouponInfo();
            String couponCode1 = couponInfo1.get(0).get("CouponCode").toString();
            couponCodesList.add(couponCode1);
            couponInfoMap.put(couponCode1, couponInfo1.get(0));

            // 平台链接优惠券
            List<Map> couponInfo2 = couponbatchWapper.getTop1PlatformLinkCouponInfo();
            String couponCode2 = couponInfo2.get(0).get("sCouponCode").toString();
            couponCodesList.add(couponCode2);
            couponInfoMap.put(couponCode2, couponInfo2.get(0));

            // 商家全场优惠券
            List<Map> couponInfo3 = couponbatchWapper.getTop1SellerCouponInfo();
            couponCodesList.add(couponInfo3.get(0).get("sCouponCode").toString());
            String couponCode3 = couponInfo3.get(0).get("sCouponCode").toString();
            couponCodesList.add(couponCode3);
            couponInfoMap.put(couponCode3, couponInfo3.get(0));

            // 商家商品优惠券
            List<Map> couponInfo4 = couponbatchWapper.getTop1SellerProductCouponInfo();
            couponCodesList.add(couponInfo4.get(0).get("sCouponCode").toString());
            String couponCode4 = couponInfo4.get(0).get("sCouponCode").toString();
            couponCodesList.add(couponCode4);
            couponInfoMap.put(couponCode4, couponInfo4.get(0));

            getcouponlistbycodeBean.setCouponCodes(couponCodesList);
            getcouponlistbycodeCall.setData(getcouponlistbycodeBean);
            getcouponlistbycodeCall.callService();

            verifyReturn(couponInfoMap);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }
    
    
    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetCouponListByCode_006() {
        Logger.start(true, "根据优惠券编号获取优惠券信息-平台活动优惠券，JIRACONN-23166");
        try {

         	
        	CouponShop2cnMCallService CouponShop2cnMCallService = new CouponShop2cnMCallService();
			SellerCreateBatchBean sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatch_ActivityBean();
			SellerCreateBatchCall sellerCreateBatchCall = CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			String batchCode=sellerCreateBatchCall.getBatchCode();
			UserReceiveCouponBean userreceivecouponBean = CouponShop2cnMCallService.setUserReceiveCouponBean(batchCode);
			UserReceiveCouponCall userReceiveCouponCall=CouponShop2cnMCallService.receiveCouponInfo(batchCode,TestDataManager.getRandomIntNum(8));
			
			HashMap<String, Map> couponInfoMap = new HashMap<String, Map>();
            List<String> couponCodesList = new ArrayList<String>();
            couponbatchWapper couponbatchWapper = new couponbatchWapper();

            // 商家全场优惠券
            List<Map> couponInfo3 = couponbatchWapper.getTop1SellerCouponInfo();
            couponCodesList.add(couponInfo3.get(0).get("CouponCode").toString());
            String couponCode3 = couponInfo3.get(0).get("CouponCode").toString();
            couponCodesList.add(couponCode3);
            couponInfoMap.put(couponCode3, couponInfo3.get(0));

            getcouponlistbycodeBean.setCouponCodes(couponCodesList);
            getcouponlistbycodeCall.setData(getcouponlistbycodeBean);
            getcouponlistbycodeCall.callService();

            verifyReturn(couponInfoMap);
            
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
    
    
    
    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetCouponListByCode_007() {
        Logger.start(true, "根据优惠券编号获取优惠券信息-平台活动优惠券，isReturnProductId=true,返回适用商品编号");
        try {
            List<String> list = new ArrayList<String>();
            list.add("qaffsfs");
            list.add("qaffsfsewrwer");
            list.add("qaffsfswerwqerqwrqw");
        	CouponShop2cnMCallService CouponShop2cnMCallService = new CouponShop2cnMCallService();
        	CreateCouponBean createCouponBean = CouponShop2cnMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
        	createCouponBean.setProductIds(list);//指定商品
        	CreateCouponCall createCouponCall = CouponShop2cnMCallService.createPlatformCoupon(createCouponBean);
			String batchCode=createCouponCall.getBatchCode();
			
			CouponShop2cnMCallService.setUserReceiveCouponBean(batchCode);
			CouponShop2cnMCallService.receiveCouponInfo(batchCode,TestDataManager.getRandomIntNum(8));
			
			HashMap<String, Map> couponInfoMap = new HashMap<String, Map>();
            List<String> couponCodesList = new ArrayList<String>();

            // 平台优惠券
            List<Map> couponInfo3 = new couponWapper().selectCouponInfoByBatchCode(batchCode);
            couponCodesList.add(couponInfo3.get(0).get("CouponCode").toString());
            String couponCode3 = couponInfo3.get(0).get("CouponCode").toString();
            couponCodesList.add(couponCode3);
            couponInfoMap.put(couponCode3, couponInfo3.get(0));

            getcouponlistbycodeBean.setCouponCodes(couponCodesList);
            getcouponlistbycodeBean.setIsReturnProductId(true);
            getcouponlistbycodeCall.setData(getcouponlistbycodeBean);
            getcouponlistbycodeCall.callService();

            verifyReturn(couponInfoMap);
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    private void verifyReturn(HashMap<String, Map> couponInfoMap) throws ParseException
    {
        Logger.verifyEquals("0", getcouponlistbycodeCall.getStatusCode1(), "验证返回Code");
        Logger.verifyEquals("操作成功！",
                getcouponlistbycodeCall.getMessage(), "验证返回Message");

        CouponInfos couponInfos = getcouponlistbycodeCall.getCouponInfos();

        for (CouponInfo couponInfo : couponInfos.getCouponInfos()) {

            Logger.comment("验证CouponCode为" + couponInfo.getCouponCode() + "记录详细信息");
            Iterator iter = couponInfoMap.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                Object key = entry.getKey();

                if (key.toString().equals(couponInfo.getCouponCode()))
                {
                    Map val = (Map) entry.getValue();

                    Map result = new couponWapper().selectCouponInfoByCouponCode(couponInfo.getCouponCode()).get(0);
                	Logger.verifyEquals(Integer.parseInt(result.get("UseConditionType").toString()), couponInfo.getProductLimitType(),
            				"验证返回字段productLimitType与CouponBatch表中UseConditionType一致");
                	//新增字段 JIRACONN-23166
                	Logger.verifyEquals(Integer.parseInt(result.get("CouponType").toString()), couponInfo.getCouponType(),
            				"验证返回字段CouponType与CouponBatch表中CouponType一致");
                	Logger.verifyEquals(result.get("CouponTypeBizValue").toString(), couponInfo.getCouponTypeBizValue(),
            				"验证返回字段CouponTypeBizValue与CouponBatch表中CouponTypeBizValue一致");
                	
                    if (val.get("CouponName") != null && !val.get("CouponName").equals(""))
                    {
                        Logger.verifyEquals(val.get("CouponName").toString(), couponInfo.getCouponName(),
                                "验证返回CouponName与数据库表CouponBatch中CouponName一致");
                    }
                    else {
                        Logger.verifyIsNull(couponInfo.getCouponName(),
                                "验证返回CouponName是Null");
                    }
                    Logger.verifyEquals(
                            0,
                            couponInfo.getMeetAmount().compareTo(
                                    new BigDecimal(val.get("MinOrderValue").toString())),
                            "验证返回MeetAmount与CouponBatch中一致");
                    Logger.verifyEquals(
                            0,
                            couponInfo.getDeductionAmount().compareTo(
                                    new BigDecimal(val.get("CouponValue").toString())),
                            "验证返回DeductionAmount与CouponBatch中一致");
                    Logger.verifyEquals(YMTDateUtil.convertDateStrToAnotherFormat(
                            val.get("ValidStart").toString().substring(0, 19), "yyyy-MM-dd HH:mm:ss",
                            "yyyy-MM-dd'T'HH:mm:ss"),
                            couponInfo.getStartTime().substring(0, 19), "验证返回StartTime");
                    Logger.verifyEquals(
                            YMTDateUtil.convertDateStrToAnotherFormat(
                                    val.get("ValidEnd").toString().substring(0, 19).substring(0, 19),
                                    "yyyy-MM-dd HH:mm:ss",
                                    "yyyy-MM-dd'T'HH:mm:ss"), couponInfo.getEndTime().substring(0, 19),
                            "验证返回EndTime");
                    
                    couponuseconditionWapper cwapper=new couponuseconditionWapper();
                    List<couponusecondition> clst=cwapper.selectByBatchId(Integer.parseInt(val.get("BatchId").toString()));
                    if(clst.size()>0)
                    {
                    	String str="";
                    	for(int i=0;i<clst.size();i++)
                    	{
                    		if((clst.get(i).getConditionType()==1 && couponInfo.getCouponType()==3) 
                    				|| (getcouponlistbycodeBean.getIsReturnProductId()==true && clst.get(0).getConditionType()== 2))
                    		{
                    		   str+=clst.get(i).getConditionValue()+",";
                    		   if(clst.get(i).getConditionType()==1 && couponInfo.getCouponType()==3)
                    			   break;
                    		   else
                    		      continue;
                    		}
                    		else
                    			 str+=clst.get(i).getConditionValue()+",";
                    	}
                        str=str.substring(0, str.length()-1);
                    	if(clst.get(0).getConditionType()==1)
                    	{
                    		
                    		Logger.verifyEquals(str, couponInfo.getSellerIds(),
                                    "验证返回sellerIds");
                    	}
                    	else {
                            Logger.verifyIsNull(couponInfo.getSellerIds(),
                                    "验证返回SellerIds为空");
                        }
                    	if (clst.get(0).getConditionType()== 4)
                        {
                            Logger.verifyEquals(str,
                                    couponInfo.getProductCategories(),
                                    "验证返回productCategories");
                        }
                        else {
                            Logger.verifyIsNull(couponInfo.getProductCategories(),
                                    "验证返回productCategories为空");
                        }
                    	if(getcouponlistbycodeBean.getIsReturnProductId()==true && clst.get(0).getConditionType()== 2)
                    	{
                    		Logger.verifyEquals(str, couponInfo.getSpecificProducts(),
                                    "验证返回specificProducts");
                    	}
                    	else
                    	 Logger.verifyIsNull(couponInfo.getSpecificProducts(),
                                 "验证返回specificProducts为空");
                    	 
                    	 
                    	 if (clst.get(0).getConditionType()== 6)
                         {
                             Logger.verifyEquals(str, couponInfo.getProductBrands(),
                                     "验证返回productBrands");
                         }
                         else {
                             Logger.verifyIsNull(couponInfo.getProductBrands(),
                                     "验证返回productBrands为空");
                         }

                         if (clst.get(0).getConditionType() == 3)
                         {
                             Logger.verifyEquals(str, couponInfo.getActivityIds(),
                                     "验证返回activityIds");
                         }
                         else if(clst.size()>1 && clst.get(1).getConditionType() == 3 &&  couponInfo.getCouponType()==3)
                         {
                        	 Logger.verifyEquals(couponInfo.getCouponTypeBizValue(), couponInfo.getActivityIds(),
                                     "验证返回activityIds");
                         }
                         else {
                             Logger.verifyIsNull(couponInfo.getActivityIds(),
                                     "验证返回activityIds为空");
                         }

                         if (String.valueOf(val.get("UsePlatformIds")).isEmpty()==false)
                         {
                             Logger.verifyEquals(str, couponInfo.getUsePlatforms(),
                                     "验证返回usePlatforms");
                         }
                         else {
                             Logger.verifyIsNull(couponInfo.getUsePlatforms(),
                                     "验证返回usePlatforms为空");
                         }

                    }
                }
                else {
                    break;
                }
            }
        }
    }
}
